//
//  CourseVideoView.m
//  xpkc
//
//  Created by TopSageOSX on 14-8-25.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CourseVideoView.h"
#import "DWMoviePlayerController.h"
#import "BasicButton.h"
#import "BottomView.h"

#import <AVFoundation/AVFoundation.h>

@interface CourseVideoView (){
    BasicButton *closeDownBtn;//向下的箭头表示收起下载
    UIScrollView *bottomScroll;//装载视频列表和提问列表的视图
    UIView *bottomView;//播放器下视图
    BottomView *downView;//下载显示视图
    
    UIView *menuLine;
    UIButton *listBtn;
    UIButton *questBtn;
    UIView *askView;
    
    BOOL volumeFlag;
    BOOL progressFlag;
    BOOL volumeChange;
    BOOL progressChange;
    CGFloat progressFlagValue;
    CGFloat volumeFlagValue;
    CGFloat progressOrder;
    CGFloat volumeOrder;
    
    
    //原进度
//    CGFloat progressSlider;
    
    /**
     *  进度标记
     */
//    CGFloat progressFlag;
//    CGFloat progressFlagValue;
}

@end
@implementation CourseVideoView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //FIXME:忽略静音
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [self creatView];
    }
    return self;
}

-(void)creatView{
    
    //设定位置
    CGRect frame_player;
    CGRect frame_menu_mini;
    CGRect frame_menu_full;
    CGRect frame_bottom;
    CGRect frame_scroll;
    CGRect frame_bottom_menu;
    CGRect frame_btn_list;
    CGRect frame_btn_quest;
    CGRect frame_btn_line;
    CGRect frame_tab_list;
    CGRect frame_tab_quest;
    CGRect frame_bottom_down;
    CGRect frame_btn_play;
    CGRect frame_label_note;
    CGRect frame_ask;
    CGRect frame_ask_filed;
    CGRect frame_ask_btn;
    
    frame_player        = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH *SCREEN_WIDTH/SCREEN_HEIGHT);
    frame_menu_mini     = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH *SCREEN_WIDTH/SCREEN_HEIGHT);
    frame_menu_full     = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH *SCREEN_WIDTH/SCREEN_HEIGHT);
    frame_bottom        = CGRectMake(0, SCREEN_WIDTH *SCREEN_WIDTH/SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - SCREEN_WIDTH *SCREEN_WIDTH/SCREEN_HEIGHT);
    frame_bottom_menu   = CGRectMake(0, 0, SCREEN_WIDTH, 45);
    frame_btn_list      = CGRectMake((SCREEN_WIDTH/2-50)/2, 0, 45, 45);
    frame_btn_quest     = CGRectMake((SCREEN_WIDTH/2-50)/2+SCREEN_WIDTH/2, 0, 45, 45);
    frame_btn_line      = CGRectMake(frame_btn_list.origin.x, 43, 45, 2);
    frame_scroll        = CGRectMake(0, 45, SCREEN_WIDTH, frame_bottom.size.height - 45);
    frame_tab_list      = CGRectMake(0, 0, SCREEN_WIDTH, frame_scroll.size.height);
    frame_tab_quest     = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, frame_scroll.size.height-40);
    frame_bottom_down   = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 49+25);//pad49+49
    frame_btn_play      = CGRectMake(SCREEN_WIDTH - 60, SCREEN_WIDTH * SCREEN_WIDTH/SCREEN_HEIGHT - 45 - 40, 50, 40);
    frame_label_note    = CGRectMake(0, SCREEN_WIDTH-100, 100, 40);
    frame_ask           = CGRectMake(SCREEN_WIDTH, frame_scroll.size.height-40, SCREEN_WIDTH, 40);
    frame_ask_filed     = CGRectMake(0, 0, SCREEN_WIDTH-75, 40);
    frame_ask_btn       = CGRectMake(SCREEN_WIDTH-75, 0, 75, 40);
    
    //playerview
    self.playerView = [[UIView alloc]initWithFrame:frame_player];
    self.playerView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.playerView];
    
    
    //menuview
    self.menuMini = [[CourseVideoViewMenuMini alloc]initWithFrame:frame_menu_mini];
    
    [self.menuMini.backBtn addTarget:self action:@selector(backToCourse) forControlEvents:UIControlEventTouchUpInside];
    [self.menuMini.downBtn addTarget:self action:@selector(downBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuMini.playBtn addTarget:self action:@selector(menuPlayClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.menuMini.fullBtn addTarget:self action:@selector(fullScreenPlayer) forControlEvents:UIControlEventTouchUpInside];
    [self.menuMini.progress addTarget:self action:@selector(progressChange:) forControlEvents:UIControlEventValueChanged];
    [self.menuMini.progress addTarget:self action:@selector(progressFinish:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
    UITapGestureRecognizer *tapmini1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(menuShowOrClose:)];
    [self.menuMini addGestureRecognizer:tapmini1];
    UITapGestureRecognizer *tapmini2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playerPauseOrPlay)];
    tapmini2.numberOfTapsRequired = 2;
    [tapmini1 requireGestureRecognizerToFail:tapmini2];
    [self.menuMini addGestureRecognizer:tapmini2];
    //滑动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(VoiceAndProgress2:)];
    [self.menuMini addGestureRecognizer:pan];
    [self.playerView addSubview:self.menuMini];
    
    
    self.menuFull = [[CourseVideoViewMenuFull alloc]initWithFrame:frame_menu_full];
    self.menuFull.hidden = YES;
    self.menuFull.delegate = self;
    //    [self.menuFull.backBtn addTarget:self action:@selector(halfScreenPlayer) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.questBtn addTarget:self action:@selector(menuQuestClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //    [self.menuFull.qualityBtn addTarget:self action:@selector(menuQualityClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.favBtn addTarget:self action:@selector(menuFavClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.chaptersBtn addTarget:self action:@selector(menuChapterClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.playBtn addTarget:self action:@selector(menuPlayClick:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.menuFull.halfBtn addTarget:self action:@selector(halfScreenPlayer) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.rateBtn addTarget:self action:@selector(menuRateClick:) forControlEvents:UIControlEventTouchUpInside];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(menuShowOrClose:)];
    [self.menuFull addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playerPauseOrPlay)];
    tap2.numberOfTapsRequired = 2;
    [tap1 requireGestureRecognizerToFail:tap2];
    [self.menuFull addGestureRecognizer:tap2];
    UIPanGestureRecognizer *panfull = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(VoiceAndProgress2:)];
    [self.menuFull addGestureRecognizer:panfull];
    [self.playerView addSubview:self.menuFull];
    
    //音量
    [self.menuFull.volumeSlider addTarget:self action:@selector(volumeHadDrog:) forControlEvents:UIControlEventValueChanged];
    [self.menuFull.volumeSlider addTarget:self action:@selector(volumeHadDrog:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuFull.volumeSlider addTarget:self action:@selector(volumeHadDrog:) forControlEvents:UIControlEventTouchUpOutside];
    self.menuFull.volumeSlider.value = [MPMusicPlayerController applicationMusicPlayer].volume;
    [self.menuFull.volumeBtn addTarget:self action:@selector(menufullOrVolume:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //bottomview
    bottomView = [[UIView alloc]initWithFrame:frame_bottom];
    [self addSubview:bottomView];
    
    UIView *menuView = [[UIView alloc]initWithFrame:frame_bottom_menu];
    menuView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    [bottomView addSubview:menuView];
    
    listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    listBtn.frame = frame_btn_list;
    [listBtn setTitle:@"章节" forState:UIControlStateNormal];
    [listBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [listBtn setTitleColor:GreenColor forState:UIControlStateSelected];
    listBtn.selected = YES;//默认选中
    [listBtn addTarget:self  action:@selector(listBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [listBtn addTarget:self action:@selector(listBtnTap:) forControlEvents:UIControlEventTouchDown];
    [menuView addSubview:listBtn];
    
    questBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    questBtn.frame = frame_btn_quest;
    [questBtn setTitle:@"提问" forState:UIControlStateNormal];
    [questBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [questBtn setTitleColor:GreenColor forState:UIControlStateSelected];
    [questBtn addTarget:self action:@selector(questBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [questBtn addTarget:self action:@selector(questBtnTap:) forControlEvents:UIControlEventTouchDown];
    [menuView addSubview:questBtn];
    
    menuLine = [[UIView alloc]initWithFrame:frame_btn_line];
    menuLine.backgroundColor = GreenColor;
    [menuView addSubview:menuLine];
    
    closeDownBtn = [BasicButton buttonWithType:UIButtonTypeCustom];
    closeDownBtn.frame = frame_bottom_menu;
    closeDownBtn.backgroundColor = GreenColor;
    closeDownBtn.alpha = 0;
    [closeDownBtn setTitle:@"取消下载" forState:UIControlStateNormal];
    [closeDownBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    closeDownBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [closeDownBtn addTarget:self action:@selector(closeDownBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:closeDownBtn];
    
    bottomScroll = [[UIScrollView alloc]initWithFrame:frame_scroll];
    bottomScroll.contentSize = CGSizeMake(SCREEN_WIDTH*2, bottomView.height-45);
    bottomScroll.showsHorizontalScrollIndicator = NO;
    bottomScroll.pagingEnabled = YES;
    bottomScroll.backgroundColor = [UIColor whiteColor];
    [bottomView addSubview:bottomScroll];
    
    self.listTab = [[UITableView alloc]initWithFrame:frame_tab_list style:UITableViewStylePlain];
    self.listTab.tag = 1;
    self.listTab.tintColor = GreenColor;
    [bottomScroll addSubview:self.listTab];
    
    self.questTab = [[UITableView alloc]initWithFrame:frame_tab_quest style:UITableViewStyleGrouped];
    self.questTab.tag = 2;
    self.questTab.separatorStyle = UITableViewCellSelectionStyleNone;
    [bottomScroll addSubview:self.questTab];
    
    askView = [[UIView alloc]initWithFrame:frame_ask];
    askView.backgroundColor = CCOLOR;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeKeyBoard)];
    [askView addGestureRecognizer:tap];
    [bottomScroll addSubview:askView];
    
    self.askFiled = [[UITextField alloc]initWithFrame:frame_ask_filed];
    self.askFiled.delegate = self;
    self.askFiled.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    [askView addSubview:self.askFiled];
    
    self.askBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.askBtn.frame = frame_ask_btn;
    self.askBtn.backgroundColor = OrangeColor;
    [self.askBtn setTitle:@"发送" forState:UIControlStateNormal];
    [self.askBtn setTitle:@"发送中" forState:UIControlStateSelected];
    [self.askBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [askView addSubview:self.askBtn];
    
    //下载菜单
    downView = [[BottomView alloc]initWithFrame:frame_bottom_down];
    [downView.leftBtn setTitle:@"全选" forState:UIControlStateNormal];
    [downView.leftBtn setTitle:@"取消全选" forState:UIControlStateSelected];
    [downView.leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [downView.leftBtn addTarget:self action:@selector(downChooseAll:) forControlEvents:UIControlEventTouchUpInside];
    [downView.rightBtn setTitle:@"开始下载" forState:UIControlStateNormal];
    [downView.rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [downView.rightBtn addTarget:self action:@selector(downStartDownload:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:downView];
    
    [self bringSubviewToFront:self.playerView];
    
    volumeChange  = YES;
    progressChange = YES;
    progressFlag = YES;
    volumeFlag = YES;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UIKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UIKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}



/**
 *  音量拖动
 *
 *  @param slider slider
 */
- (void)volumeHadDrog:(UISlider *)slider{
    //    hideTime = 5;
    [MPMusicPlayerController applicationMusicPlayer].volume = slider.value;
    //触发静音事件
    if (slider.value == 0) {
        self.menuFull.volumeBtn.selected = YES;
    }else{
        self.menuFull.volumeBtn.selected = NO;
    }
}


/**
 *  音量停止拖动
 *
 *  @param slider slider
 */
- (void)volumeEndDrog:(UISlider *)slider{
    [MPMusicPlayerController applicationMusicPlayer].volume = slider.value;
    if (slider.value == 0) {
        self.menuFull.volumeBtn.selected = YES;
    }else{
        self.menuFull.volumeBtn.selected = NO;
    }
}

/**
 *  静音按钮
 *
 *  @param btn 按钮
 */
- (void)menufullOrVolume:(UIButton *)btn{
    if (btn.selected) {
        btn.selected = NO;
        [MPMusicPlayerController applicationMusicPlayer].volume = 0.3;
        self.menuFull.volumeSlider.value = 0.3;
    }else{
        //静音
        btn.selected = YES;
        [MPMusicPlayerController applicationMusicPlayer].volume = 0.0;
        self.menuFull.volumeSlider.value = 0.0;
    }
}


/**
 *  音量加减 播放进度条
 *
 *  @param span 滑动
 */
-(void)VoiceAndProgress2:(UIPanGestureRecognizer *)pan{
   
#if 0
    //    progressFlag = 0;
    //    volumeFlag = 0;
    //    if (ABS([pan translationInView:self.menuFull].y)>ABS([pan translationInView:self.menuFull].x)+5) {
    //        //确定是调整声音
    //        progressFlag = 0;
    //        volumeFlag = 1;
    //    }
    //    if (ABS([pan translationInView:self.menuFull].x)>ABS([pan translationInView:self.menuFull].y)+5){
    //        //确定是调整进度
    //        volumeFlag = 0;
    //        progressFlag = 1;
    //    }
    //获取变化进度
    progressFlagValue = [pan translationInView:self.menuFull].x;
    volumeFlagValue = [pan translationInView:self.menuFull].y;
    CGFloat dragProgress = progressFlagValue/SCREEN_HEIGHT*0.3;
    CGFloat dragVolume = -volumeFlagValue/SCREEN_WIDTH;
    //开始拖动
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        //        logdebug(@"开始滑动");
        //        logdebug(@"%f/%f",
        //                 menuview.progressSlider.value,SCREEN_HEIGHT);
        
        //记录当前进度条位置
        //        progressSlider = self.menuFull.progressSlider.value;
        //记录当前声音大小
        volumeOrder = [MPMusicPlayerController applicationMusicPlayer].volume;
    }
    /////
    //调整声音
    //
    if (volumeFlag == 1&&volumeChange) {
        logdebug(@"调节音量");
        [MPMusicPlayerController applicationMusicPlayer].volume = volumeOrder+dragVolume;
        self.menuFull.volumeSlider.value = volumeOrder+dragVolume;
        //禁止进度调整
        progressChange = NO;
        //触发静音
        if ([MPMusicPlayerController applicationMusicPlayer].volume == 0.0) {
            self.menuFull.volumeBtn.selected = YES;
        }else{
            self.menuFull.volumeBtn.selected = NO;
        }
    }
    
    ////
    //调整进度
    //
    if (progressFlag == 1&&progressChange) {
        logdebug(@"调节进度");
        //暂停播放器
        //        [player pause];
        //        //暂停计时器
        //        [self.mainTimer setFireDate:[NSDate distantFuture]];
        //        //显示菜单
        //        if (menuanimationtype == MenuHidenOrShow) {
        //            [menuview menuShow];
        //        }else{
        //            [menuview menuIn];
        //        }
        //        hideTime = 3;
        //        // 超出时长计算
        //        if ((dragProgress+self.player.currentPlaybackTime/self.player.duration)>1) {
        //            //
        //            dragProgress = (self.player.duration-self.player.currentPlaybackTime)/self.player.duration;
        //        }
        //        menuview.noteLabel.text = [NSString stringWithFormat:@"%@%02d:%02d",dragProgress>0?@"前进":@"后退",(int)ABS(dragProgress*self.player.duration)/60,(int)ABS(dragProgress*self.player.duration)%60];
        //        menuview.noteLabel.hidden = NO;
        //        menuview.progressSlider.value = progressSlider+dragProgress;
        //拖动进度的过程中禁止音量变化
        volumeChange = NO;
    }
    if (pan.state ==  UIGestureRecognizerStateEnded) {
        if (progressChange) {
            //继续播放
            logdebug(@"结束滑动");
            //            play = YES;
            //            [menuview.playBtn setImage:[UIImage imageNamed:@"小窗_stop_media"] forState:UIControlStateNormal];
            //            if (isPad) {
            //                [menuview.playBtn setImage:[UIImage imageNamed:@"ic_stop_media"] forState:UIControlStateNormal];
            //            }
            //            self.player.currentPlaybackTime = menuview.progressSlider.value*self.player.duration;
            //            [self.player play];
            //            [self.mainTimer setFireDate:[NSDate date]];
            //结束进度的变化时可以调节音量
            volumeChange = YES;
        }
        if (volumeChange) {
            progressChange = YES;
        }
    }
#else
    
    progressFlag = 0;
    volumeFlag = 0;
    if (ABS([pan translationInView:pan.view].y)>ABS([pan translationInView:pan.view].x)+5) {
        //确定是调整声音
        progressFlag = 0;
        volumeFlag = 1;
    }
    if (ABS([pan translationInView:pan.view].x)>ABS([pan translationInView:pan.view].y)+5){
        //确定是调整进度
        volumeFlag = 0;
        progressFlag = 1;
    }
    
    //获取变化进度
    progressFlagValue = [pan translationInView:pan.view].x;
    volumeFlagValue = [pan translationInView:pan.view].y;
    logdebug(@"\nx:%f\ny:%f",progressFlagValue,volumeFlagValue);
    logdebug(@"%f----%f",SCREEN_WIDTH,SCREEN_HEIGHT);
    CGFloat dragProgress = progressFlagValue/SCREEN_HEIGHT*0.3;
    CGFloat dragVolume =  - volumeFlagValue/SCREEN_WIDTH;
    //开始拖动
    if (pan.state == UIGestureRecognizerStateBegan) {
        
        logdebug(@"开始滑动");
        //        logdebug(@"%f/%f",menuview.progressSlider.value,SCREEN_HEIGHT);
        
        //记录当前进度条位置
        progressOrder = self.menuMini.progress.value;
        progressOrder = self.menuFull.progressView.value;
        //记录当前声音大小
        volumeOrder = [MPMusicPlayerController applicationMusicPlayer].volume;
    }
    if (progressFlag&&volumeFlag) {
        if (ABS([pan translationInView:pan.view].y)>ABS([pan translationInView:pan.view].x)+2) {
            //确定是调整声音
            progressFlag = NO;
            volumeFlag = YES;
        }
        if (ABS([pan translationInView:pan.view].x)>ABS([pan translationInView:pan.view].y)+2){
            //确定是调整进度
            volumeFlag = NO;
            progressFlag = YES;
        }
    }
    /////
    //调整声音
    //
    if (volumeFlag == 1&&volumeChange) {
        logdebug(@"调节音量");
        [MPMusicPlayerController applicationMusicPlayer].volume = volumeOrder+dragVolume;
        //禁止进度调整
        progressChange = NO;
        self.menuFull.volumeSlider.value = volumeOrder+dragVolume;
        //禁止进度调整
        progressChange = NO;
        //触发静音
        if ([MPMusicPlayerController applicationMusicPlayer].volume == 0.0) {
            self.menuFull.volumeBtn.selected = YES;
        }else{
            self.menuFull.volumeBtn.selected = NO;
        }
    }
    
    ////
    //调整进度
    //
    if (progressFlag&&progressChange) {
        logdebug(@"调节进度");
        //暂停播放器
        [self playerPauseView];
        self.menuMini.progress.value = progressOrder + dragProgress;
        self.menuFull.progressView.value = progressOrder + dragProgress;
        
        //暂停计时器
        //        [self.mainTimer setFireDate:[NSDate distantFuture]];
        //显示菜单
        //        if (menuanimationtype == MenuHidenOrShow) {
        //            [menuview menuShow];
        //        }else{
        //            [menuview menuIn];
        //        }
        //        hideTime = 3;
        //超出时长计算
        //        if ((dragProgress+self.player.currentPlaybackTime/self.player.duration)>1) {
        //            //
        //            dragProgress = (self.player.duration-self.player.currentPlaybackTime)/self.player.duration;
        //        }
        //        menuview.noteLabel.text = [NSString stringWithFormat:@"%@%02d:%02d",dragProgress>0?@"前进":@"后退",(int)ABS(dragProgress*self.player.duration)/60,(int)ABS(dragProgress*self.player.duration)%60];
        //        menuview.noteLabel.hidden = NO;
        //        menuview.progressSlider.value = progressSlider+dragProgress;
        //拖动进度的过程中禁止音量变化
        volumeChange = NO;
    }
    if (pan.state ==  UIGestureRecognizerStateEnded) {
        volumeFlag = YES;
        progressFlag = YES;
        if (progressChange) {
            //继续播放
            logdebug(@"结束滑动");
            [self.delegate playerSetProgress:self.menuFull.progressView.value];
//            play = YES;
//            [self.menuMini.playBtn setImage:[UIImage imageNamed:@"小窗_stop_media"] forState:UIControlStateNormal];
//            if (isPad) {
//                [self.menuMini.playBtn setImage:[UIImage imageNamed:@"ic_stop_media"] forState:UIControlStateNormal];
//            }
//            player.currentPlaybackTime = menuview.progressSlider.value*self.player.duration;
//            [self.player play];
//            [self.mainTimer setFireDate:[NSDate date]];
            //结束进度的变化时可以调节音量
            volumeChange = YES;
        }
        if (volumeChange) {
            progressChange = YES;
        }
    }

    
#endif

}

#pragma mark - menu func
- (void)backToCourse{
    if ([self.delegate respondsToSelector:@selector(backToCourseVC)]) {
        [self.delegate backToCourseVC];
    }
}
//- (void)fullScreenPlayer{
//    self.menuMini.hidden = YES;
//    self.menuFull.hidden = NO;
//    [UIView animateWithDuration:0.3 animations:^{
//        self.playerView.transform = CGAffineTransformMakeRotation(M_PI_2* 5);
//        self.playerView.transform = CGAffineTransformTranslate(self.playerView.transform, SCREEN_HEIGHT/2-SCREEN_WIDTH*SCREEN_WIDTH/SCREEN_HEIGHT/2, 0);
//        self.playerView.transform = CGAffineTransformScale(self.playerView.transform, SCREEN_HEIGHT/SCREEN_WIDTH, SCREEN_HEIGHT/SCREEN_WIDTH);
//
//        self.menuFull.progressView.transform = CGAffineTransformMakeTranslation(-(SCREEN_HEIGHT-SCREEN_WIDTH)/2, 0);
//        self.menuFull.progressView.transform = CGAffineTransformScale(self.menuFull.progressView.transform, SCREEN_WIDTH/SCREEN_HEIGHT, SCREEN_WIDTH/SCREEN_HEIGHT);
//    }];
//
//
//
//    [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
//}
//- (void)halfScreenPlayer{
//    self.menuMini.hidden = NO;
//    self.menuFull.hidden = YES;
//    [UIView animateWithDuration:0.3 animations:^{
//        CGAffineTransform transform = CGAffineTransformMakeRotation(0);
//        transform = CGAffineTransformScale(transform, 1, 1);
//        transform = CGAffineTransformTranslate(transform, 0, 0);
//        self.playerView.transform = transform;
//
//
//
//    }];
//    [[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
//}

- (void)downBtnClick:(UIButton *)btn{
    
    [self bringSubviewToFront:bottomView];
    [UIView animateWithDuration:0.4 animations:^{
        bottomView.top = 0;
        bottomView.height = SCREEN_HEIGHT - downView.height;
        bottomScroll.height = SCREEN_HEIGHT - 45 - downView.height;
        self.listTab.height = SCREEN_HEIGHT - 45 - downView.height;
        self.questTab.height = SCREEN_HEIGHT - 45 - downView.height;
        downView.top = bottomView.bottom;
        closeDownBtn.alpha = 1;
        [downView showAnimationWithTime:0.4 andDely:0.0];
    }completion:^(BOOL finished) {
        if ([self.delegate respondsToSelector:@selector(downBtnClick:)]) {
            [self.delegate downBtnClick:btn];
            [self playerPauseView];
            [self listBtnClick:btn];
        }
    }];
    NSFileManager *manager =
    [NSFileManager defaultManager];
    NSDictionary * infoDic = [manager fileSystemAttributesAtPath:NSHomeDirectory()];
    float usedsize = [[infoDic objectForKey:@"NSFileSystemSize"]integerValue]/1024.0/1024.0-[[infoDic objectForKey:@"NSFileSystemFreeSize"]integerValue]/1024.0/1024.0;
    float mediasize = [GetFileSize folderSizeAtPath:DOC_PATH];
    logdebug(@"视频大小%f",mediasize);
    float freesize = [[infoDic objectForKey:@"NSFileSystemFreeSize"]integerValue]/1024.0/1024.0;
    [downView setColorWith:usedsize andMedia:mediasize andFree:freesize];
    downView.noteLabel.text = [NSString stringWithFormat:@"已用:%0.1fG(学派视频:%0.1fM)/空闲:%0.1fG",usedsize/1024.0,mediasize,freesize/1024.0];
    downView.noteLabel.font = FONT_SYS_NORMAL;
}
- (void)closeDownBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(closeDownBtnClick:)]) {
        [self.delegate closeDownBtnClick:btn];
    }
    [UIView animateWithDuration:0.4 animations:^{
        bottomView.top = SCREEN_WIDTH*SCREEN_WIDTH/SCREEN_HEIGHT;
        closeDownBtn.alpha = 0;
    }completion:^(BOOL finished) {
        bottomView.height = SCREEN_HEIGHT - bottomView.top;
        bottomScroll.height = bottomView.height - 45;
        self.listTab.height = bottomView.height - 45;
        self.questTab.height = bottomView.height - 45;
        
        [self bringSubviewToFront:self.playerView];
    }];
    [downView hideAnimationWithTime:0.8 andDely:0.0];
    [downView hideColor];
    downView.leftBtn.selected = NO;
}
- (void)menuPlayClick:(UIButton *)btn{
    if (self.isPlay) {
        [self playerPauseView];
    }else{
        [self playerPlayView];
    }
}
- (void)menuQuestClick:(UIButton *)btn{
    [self.menuFull.halfDelegate halfScreenPlayer];
    [self questBtnClick:btn];
}
//- (void)menuQualityClick:(UIButton *)btn{
//    logdebug(@"画质");
//    if (btn.selected) {
//        [self.menuFull closeQuality];
//    }else{
//        [self.menuFull openQuality];
//    }
//}

- (void)menuRateClick:(UIButton *)btn{
    logdebug(@"倍速");
    if (btn.selected) {
        [self.menuFull closeRate];
    }else{
        [self.menuFull openRate];
    }
}

- (void)menuChapterClick:(UIButton *)btn{
    logdebug(@"章节");
    [self showChapterView];
}
- (void)menuFavClick:(UIButton *)btn{
    logdebug(@"关注");
    if (btn.selected) {
        btn.selected = NO;
    }else{
        btn.selected = YES;
    }
}
- (void)downChooseAll:(UIButton *)btn{
    logdebug(@"全选");
    if ([self.delegate respondsToSelector:@selector(downChooseAll:)]) {
        [self.delegate downChooseAll:btn];
    }
}
- (void)downStartDownload:(UIButton *)btn{
    logdebug(@"开始下载");
    if ([self.delegate respondsToSelector:@selector(downStartDownload:)]) {
        [self.delegate downStartDownload:btn];
    }
    [self closeDownBtnClick:btn];
}
- (void)showChapterView{
    [self.menuFull openChapter];
}
- (void)closeChapterView{
    [self.menuFull closeChapter];
    
}
- (void)menuShowOrClose:(UIGestureRecognizer *)ges{
    if (ges.view == self.menuFull) {
        if (self.menuFull.isClose) {
            if (!self.menuFull.isChapterClose) {
                [self.menuFull closeChapter];
            }else{
                [self.menuFull openMenu];
            }
        }else{
            [self.menuFull closeMenu];
        }
    }else{
        if (self.menuMini.isHide) {
            [self.menuMini showMenu];
        }else{
            [self.menuMini hideMenu];
        }
    }
}



#pragma mark - fulldelegate
- (void)qualityBtnClick:(UIButton *)btn{
    logdebug(@"选择画质");
    logdebug(@"%@",btn.titleLabel.text);
    [self.delegate playerSetQuality:btn.tag];
    
}
- (void)rateBtnClick:(UIButton *)btn{
    logdebug(@"选择速率");
    logdebug(@"%@",btn.titleLabel.text);
    if (![self.delegate respondsToSelector:@selector(playerSetRate:)]) {
        return;
    }
    switch (btn.tag) {
        case PlayerRateSlow:
        {
            logdebug(@"0.8");
            [self.delegate playerSetRate:0.8];
            self.menuFull.currentRate = PlayerRateSlow;
            [self.menuFull.rateBtn setTitle:@"0.8X" forState:UIControlStateNormal];
        }
            break;
        case PlayerRateNormal:
        {
            logdebug(@"1.0");
            [self.delegate playerSetRate:1.0];
            self.menuFull.currentRate = PlayerRateNormal;
            [self.menuFull.rateBtn setTitle:@"1.0X" forState:UIControlStateNormal];
        }
            break;
        case PlayerRateFast:
        {
            logdebug(@"1.5");
            [self.delegate playerSetRate:1.5];
            self.menuFull.currentRate = PlayerRateFast;
            [self.menuFull.rateBtn setTitle:@"1.5X" forState:UIControlStateNormal];
        }
            break;
            
        default:
            break;
    }
}
- (void)chapterSelectedAtIndex:(NSIndexPath *)indexpath{
    if ([self.delegate respondsToSelector:@selector(playerChangeToSection:andRow:)]) {
        [self.delegate playerChangeToSection:indexpath.section andRow:indexpath.row];
    }
}
#pragma mark - -
- (void)playerPauseOrPlay{
    if (self.isPlay) {
        [self playerPauseView];
    }else{
        [self playerPlayView];
    }
}
- (void)playerPlayView{
    [self.delegate playerPlay];
    self.menuFull.playBtn.selected = YES;
    self.menuMini.playBtn.selected = YES;
}
- (void)playerPauseView{
    [self.delegate playerPause];
    self.menuFull.playBtn.selected = NO;
    self.menuMini.playBtn.selected = NO;
    
}
- (void)setProgressValue:(CGFloat)progress{
    self.menuMini.progress.value = progress;
    self.menuFull.progressView.value = progress;
}

- (void)progressChange:(UISlider *)slider{
    logdebug(@"进度%f",slider.value);
}
- (void)progressFinish:(UISlider *)slider{
    logdebug(@"结束%f",slider.value);
    [self.delegate playerSetProgress:slider.value];
}



#pragma mark - bottom
- (void)listBtnClick:(UIButton *)btn{
    [UIView animateWithDuration:0.4 animations:^{
        listBtn.selected = YES;
        questBtn.selected = NO;
        menuLine.left = listBtn.left;
        menuLine.width = 45;
        bottomScroll.contentOffset = CGPointMake(0, 0);
    }];
    
}
- (void)listBtnTap:(UIButton *)btn{
    if (btn.selected) {
        //
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            menuLine.width = questBtn.right - listBtn.left;
            menuLine.left = listBtn.left;
        }];
    }
}
- (void)questBtnClick:(UIButton *)btn{
    [UIView animateWithDuration:0.4 animations:^{
        listBtn.selected = NO;
        questBtn.selected = YES;
        menuLine.left = questBtn.left;
        menuLine.width = 45;
        bottomScroll.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }];
}
- (void)questBtnTap:(UIButton *)btn{
    if (btn.selected) {
        //
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            menuLine.width = questBtn.right - listBtn.left;
        }];
    }
}

#pragma mark - ask
- (void)closeKeyBoard{
    askView.height = 40;
    self.askFiled.height = 40;
    [self.askFiled resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self closeKeyBoard];
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGFloat height = [textField.text heightForString:textField.text withWidth:textField.width andFont:textField.font];
    if (height > 40) {
        askView.height = height;
        self.askFiled.height = height;
        self.askBtn.top = height - 40;
    }
}
-(void)UIKeyboardWillHideNotification:(NSNotification *)sender{
    
    self.transform=CGAffineTransformIdentity;
    
}
-(void)UIKeyboardWillShowNotification:(NSNotification *)sender{
    CGRect rect=[[sender.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
    CGFloat height= rect.size.height;
    self.transform=CGAffineTransformMakeTranslation(0, -height);
    
}

@end
